<template>
  <div class="top-menu">
    <el-menu
      :default-active="activeIndex"
      class="horizontal-menu"
      mode="horizontal"
      background-color="transparent"
      text-color="#ffffff"
      active-text-color="#ffffff"
      router>
      <template v-for="(item, index) in mainMenuItems">
        <el-menu-item :key="index" :index="item.path" v-if="!item.children || item.children.length === 0">
          <i :class="item.icon"></i>
          <span>{{ item.name }}</span>
        </el-menu-item>
        <el-submenu :key="index" :index="item.id + ''" v-else>
          <template slot="title">
            <i :class="item.icon"></i>
            <span>{{ item.name }}</span>
          </template>
          <el-menu-item 
            v-for="child in item.children" 
            :key="child.id" 
            :index="child.path">
            <i :class="child.icon"></i>
            <span>{{ child.name }}</span>
          </el-menu-item>
        </el-submenu>
      </template>
    </el-menu>
  </div>
</template>

<script>
export default {
  name: 'TopMenu',
  data() {
    return {
      activeIndex: this.$route.path,
      mainMenuItems: [
        {
          id: '1',
          name: '首页概览',
          path: '/dashboard',
          icon: 'el-icon-s-home'
        },
        {
          id: '2',
          name: '产品管理',
          icon: 'el-icon-s-goods',
          children: [
            {
              id: '2-1',
              name: '产品列表',
              path: '/products',
              icon: 'el-icon-menu'
            },
            {
              id: '2-2',
              name: '添加产品',
              path: '/products/add',
              icon: 'el-icon-plus'
            }
          ]
        },
        {
          id: '3',
          name: '订单中心',
          path: '/orders',
          icon: 'el-icon-s-order'
        },
        {
          id: '4',
          name: '用户管理',
          path: '/users',
          icon: 'el-icon-user'
        },
        {
          id: '5',
          name: '数据分析',
          path: '/analytics',
          icon: 'el-icon-data-analysis'
        }
      ]
    };
  },
  created() {
    // 可以从localStorage获取菜单数据
    const userMenuList = localStorage.getItem("userMenuList");
    if (userMenuList) {
      const menuData = JSON.parse(userMenuList);
      this.processMenuData(menuData);
    }
  },
  methods: {
    processMenuData(menuData) {
      if (menuData && menuData.length > 0) {
        this.mainMenuItems = menuData;
      }
    }
  }
};
</script>

<style scoped>
.top-menu {
  height: 100%;
  display: flex;
  align-items: center;
}

.horizontal-menu {
  border-bottom: none;
  background-color: transparent;
}

.el-menu--horizontal > .el-menu-item {
  height: 60px;
  line-height: 60px;
  color: #ffffff;
  font-size: 15px;
  padding: 0 20px;
  border-bottom: none;
}

.el-menu--horizontal > .el-menu-item.is-active {
  color: #ffffff;
  font-weight: bold;
  background-color: rgba(255, 255, 255, 0.15);
  border-bottom: 3px solid #ffffff;
}

.el-menu--horizontal > .el-submenu {
  height: 60px;
  line-height: 60px;
}

.el-menu--horizontal > .el-submenu .el-submenu__title {
  height: 60px;
  line-height: 60px;
  color: #ffffff;
  font-size: 15px;
  border-bottom: none;
}

.el-menu--horizontal > .el-submenu.is-active .el-submenu__title {
  color: #ffffff;
  font-weight: bold;
  background-color: rgba(255, 255, 255, 0.15);
  border-bottom: 3px solid #ffffff;
}

.el-menu--horizontal > .el-menu-item i,
.el-menu--horizontal > .el-submenu .el-submenu__title i {
  color: #ffffff;
  margin-right: 5px;
  font-size: 18px;
}

/* 下拉菜单样式 */
.el-menu--popup {
  min-width: 150px;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  background-color: #ffffff !important;
}

.el-menu--popup-bottom-start {
  margin-top: 5px;
}

.el-menu--popup .el-menu-item {
  height: 40px;
  line-height: 40px;
  padding: 0 20px;
  font-size: 14px;
  color: #333333 !important;
}

.el-menu--popup .el-menu-item.is-active {
  color: #ffffff !important;
  background-color: #4caf50 !important;
}

.el-menu--popup .el-menu-item:hover {
  color: #4caf50 !important;
  background-color: rgba(76, 175, 80, 0.1) !important;
}

.el-menu--popup .el-menu-item.is-active:hover {
  color: #ffffff !important;
}

.el-menu--popup .el-menu-item i {
  margin-right: 5px;
  color: #606266 !important;
}

.el-menu--popup .el-menu-item:hover i {
  color: #4caf50 !important;
}

.el-menu--popup .el-menu-item.is-active i {
  color: #ffffff !important;
}
</style> 